package com.zzh.jetpackdemo.live

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import com.zzh.jetpackdemo.R
import com.zzh.jetpackdemo.live.community.MyObserver
import kotlinx.android.synthetic.main.activity_live.*

class LiveActivity : AppCompatActivity() {

    lateinit var mMutableLiveData: MutableLiveData<String>


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_live)
        addLife()
        init()
    }

    private fun init() {
        var i = 0
        mMutableLiveData = MutableLiveData<String>()
        mMutableLiveData.observe(this, Observer {
            content.text = it
        })
        sendData.setOnClickListener {
            mMutableLiveData.value = ">>>>>${i++}"
        }
    }


    // 添加生命共同体
    private fun addLife() {
        lifecycle.addObserver(MyObserver("1"))
        lifecycle.addObserver(MyObserver("2"))
    }


}